/*
 * 地址选择
 */
'use strict';
import React, { Component } from 'react';
import {
    View,
    Text,
    StyleSheet,
    Image,
    ListView,
    RefreshControl,
    ActivityIndicator,
    TouchableOpacity,
    Dimensions,
    Animated,
    TouchableHighlight,
    AsyncStorage,
    PixelRatio,
    TextInput,
    Alert,
    ScrollView
} from 'react-native';


import Toast from 'antd-mobile/lib/toast';
import List from 'antd-mobile/lib/list';
import SearchBar from 'antd-mobile/lib/search-bar';
import Flex from 'antd-mobile/lib/flex';

import NavTitle from '../component/NavTitle';

import GridView from 'react-native-gridview';

import XFetch from '../XFetch';

import GoodsDetail from './GoodsDetail';
import ErweimaPro from './ErweimaPro';

import * as WechatAPI from 'react-native-wx';

class SearchPro extends Component {
    constructor(props) {
        super(props)
        this.state = {
            data:[]
        }
    }

    componentDidMount(){
        this._getGoods();
    }

    _getGoods(){
        XFetch(`http://www.weiweigo.com/app/goods.aspx?method=listByPage&key=${this.props.value}`,{
            method:'GET'
        }).then(data=>{
            this.setState({
                data:data.data
            })
        });
    }

    _onShowDatail(data){
        this.props.navigator.push({
            component: GoodsDetail,
            name: 'GoodsDetail',
            params:{
                id:data.id
            }
        })
    }

    _getErweima(id){
        this.props.navigator.push({
            name:'ErweimaPro',
            component:ErweimaPro,
            params:{
                id:id
            }
        })
    }

    _share(){
        WechatAPI.shareToSession({
            type: 'text',
            text: '微微购分享测试啦',
        }).then(data=>{
            console.log(data);
        })
    }


    render() {
        return (
            <View style={styles.container}>
                <NavTitle
                    title='搜索结果'
                    leftButton = {true}
                    leftImg = {require('../imgs/left.png')}
                    leftText = {'返回'}
                    leftHander = {()=>{this.props.navigator.pop()}}
                    headColor = '#fbdc3a'
                />
                <ScrollView
                    showsVerticalScrollIndicator={false}
                >
                <View style={styles.content}>
                    <GridView
                        data={this.state.data}
                        itemsPerRow={2}
                        renderItem={(item, sectionID, rowID, itemIndex, itemID) => {
                          return (
                            <Flex
                              justify="start"
                              direction="column"
                              align="start"
                              style={{flex:1,paddingTop:10,paddingLeft:itemID%2 == 0?0:5,paddingRight:itemID%2 == 0?5:0}}
                            >
                                <TouchableOpacity
                                  activeOpacity={1}
                                  onPress={() => this._onShowDatail(item)}>
                                      <Image
                                            source={{uri:`http://www.weiweigo.com${item.imgUrl}`}}
                                            resizeMode="contain"
                                            style={{width:Dimensions.get('window').width/2-15,height:160}}
                                      />
                                      <Text
                                            numberOfLines={1}
                                            style={{marginTop:5,color:'#333',fontSize:16}}
                                      >
                                        {item.title}
                                      </Text>
                                      <View style={styles.goodsInfo}>
                                        <Text numberOfLines={1} style={styles.price}>￥{item.price}</Text>
                                      </View>
                                      <Text style={styles.remark} numberOfLines={2}>{item.content}</Text>
                                      <View style={styles.goodsFooter}>
                                            <TouchableOpacity style={styles.tabGoodsClick} onPress={this._getErweima.bind(this,item.id)}>
                                                <Image style={styles.tabSmallImg} source={require('../imgs/psIcon3.png')}></Image>
                                                <Text style={styles.tabSmallText}>生成二维码</Text>
                                            </TouchableOpacity>
                                            <TouchableOpacity style={styles.tabGoodsClick} onPress={this._share.bind(this,item.id)}>
                                                <Image style={styles.tabSmallImg} source={require('../imgs/psIcon5.png')}></Image>
                                                <Text style={styles.tabSmallText}>分享推广</Text>
                                            </TouchableOpacity>
                                      </View>
                                </TouchableOpacity>
                            </Flex>
                          );
                        }}
                    />
                </View>
                </ScrollView>
            </View>
        )
    }

}

const styles = StyleSheet.create({
    container: {
        backgroundColor: '#EEEEEF',
        flex: 1,
    },
    content:{
        paddingLeft:5
    },
    goodsInfo:{
        flexDirection:'row',
        marginTop:3
    },
    scanCount:{
        fontSize:12,
        color:'#999',
    },
    price:{
        flex:1,
        fontSize:14,
        color:'#e83828',
    },
    remark:{
        color:'#b1b1b2',
        marginTop:3,
        marginBottom:3,
        paddingRight:5,
        fontSize:13,
        lineHeight:16
    },
    goodsFooter:{
        flexDirection:'row',
        justifyContent:'flex-end',
        marginTop:3
    },
    tabIcon:{
        flexDirection:'row',
        marginTop:15
    },
    tabClick:{
        flexDirection:'column',
        justifyContent:"center",
        alignItems:'center',
        marginLeft:20
    },
    tabGoodsClick:{
        flexDirection:'column',
        alignItems:'center',
        marginLeft:10
    },
    tabImg:{
        width:30,
        height:30
    },
    tabSmallImg:{
        width:22,
        height:22,
        alignSelf:'center'
    },
    tabText:{
        marginTop:8
    },
    tabSmallText:{
        marginTop:5,
        fontSize:13
    },
});

export default SearchPro;
